// pages/user/register/register.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sendtxt: "获取验证码",
        phone: null,
        phoneTest: false,
        verificationcode: null,
        verificationcodeTest: false,
        sendtxt: '发送验证码',
        gettingCode: true,
        password: null,
        passwordTest: false,
        successCallback: null,
        agreement: {
            stauts: false,
          image: "http://run.4hl.cn/addons/sd_drviing/core/public/WeChat/driver/icon-34.png"
        },
    },
    // phoneinput 获取电话号码
    phoneinput: function(event) {
        var that = this
        that.setData({
            phone: event.detail.value
        })
        this.phoneTest(event.detail.value)
    },
    // phoneTest 电话号码验证
    phoneTest: function(params) {
        if (params == '' || !/^1\d{10}$/.test(params)) {
            wx.showToast({
                title: '请输入正确的电话号码格式',
                icon: "none"
            })
            this.setData({
                phoneTest: false,
            });
            return;
        } else {
            this.setData({
                phoneTest: true,
            });
        }
    },
    // 发送验证码
    sendcode: function() {
        var that = this;
        wx.showLoading({})
        app.request({
            url: api.user.send_sms,
            method: 'post',
            data: {
                phone: that.data.phone,
            },
            success: function(rss) {
              wx.hideLoading()
                if (rss.code == 0) {
                    handler.showToast('验证码获取失败', "none")
                    return;
                } else {
                  that.setData({ gettingCode:false});
                  that.data.gettingCode = false;
                    that.countDown();
                }
            }
        })
        /*this.phoneTest(this.data.phone)
        // console.log('触发发验证码')
        wx.request({
          url: 'https://product.fishqc.com/Api/WeixinLogin/weixinminiMobileVerificationCode',
          data: {
            mobile: that.data.phone
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              that.countDown()
            } else {
              wx.showToast({
                title: '获取验证码失败',
                icon:"none"
              })
            }
          },
          fail:function(){
            wx.showToast({
              title: '获取验证码失败',
              icon: "none"
            })
          }
        })*/
    },
    //倒计时 倒数
    countDown: function() {
        var that = this;
        
        var seconds = 59;
        var countTimer = setInterval(function() {
            that.setData({
                sendtxt: seconds + "s"
            })
            seconds--;
            if (seconds <= 0) {
                countTimer = clearInterval(countTimer);
                that.setData({
                    sendtxt: '获取验证码'
                })
              that.setData({ gettingCode: true });
                that.data.gettingCode = true;
            }
        }, 1000);
    },
    verificationinput: function(e) {
        this.setData({
            verificationcode: e.detail.value,
        });
        this.verificationTest(e.detail.value);
    },
    verificationTest: function(params) {
        if (params == null) {
            wx.showToast({
                title: '请输入验证码',
                icon: "none",
                duration:5000
            })
            return;
        } else {
            this.setData({
                verificationcodeTest: true,
            });
        }
    },
    // passwordinput 获取密码
    passwordinput: function(e) {
        console.log(e.detail.value);
        this.setData({
            password: e.detail.value
        });
        this.passwordTest(e.detail.value);
    },
    // passwordTest 密码验证
    passwordTest: function(params) {
        if (params == '' || !/^\d{6}$/.test(params)) {
            wx.showToast({
                title: '请输入正确的密码格式',
                icon: "none",
                duration: 5000
            })
            return;
        } else {
            this.setData({
                passwordTest: true,
            });
        }
    },
    // getPhoneLogin 登录的电话号码
    getPhoneLogin: function() {
        var that = this;
        const {
            sendtxt,
            phone,
            phoneTest,
            verificationcode,
            verificationcodeTest,
            password,
            passwordTest,
            successCallback,
            agreement
        } = this.data;
        // console.log(sendtxt,
        //   phone,
        //   phoneTest,
        //   verificationcode,
        //   verificationcodeTest,
        //   password,
        //   passwordTest,
        //   successCallback,
        //   agreement);
        if (agreement.stauts && verificationcodeTest && passwordTest && phoneTest) {
            //司机注册
            // 获取用户信息
            wx.showLoading({})
            wx.getSetting({
                success: res => {
                    if (res.authSetting['scope.userInfo']) {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                        wx.getUserInfo({
                            success: res => {
                                console.log('获取用户信息', res)
                                // 登录
                                wx.login({
                                    success: re => {
                                        app.request({
                                            url: api.user.login,
                                            method: 'post',
                                            data: {
                                                code: re.code,
                                                iv: res.iv,
                                                encryptedData: res.encryptedData
                                            },
                                            success: function(rs) {
                                                //获取到用户详细信息
                                                if (rs.code == 1) {
                                                     wx.setStorageSync("access_token", rs.data.access_token);
                                                    app.request({
                                                        url: api.user.driver_binding_phone,
                                                        method: 'post',
                                                        data: {
                                                            code: re.code,
                                                            iv: res.iv,
                                                            encryptedData: res.encryptedData,
                                                            phone: that.data.phone,
                                                            pass: that.data.password,
                                                            type: 1, //1表示代驾人员 0表示普通用户
                                                            code: that.data.verificationcode,
                                                        },
                                                        success: function(rs) {
                                                            //获取到用户详细信息 console.log(rs)
                                                            wx.hideLoading()
                                                            if (rs.code == 0) {
                                                                wx.showToast({
                                                                    title: rs.msg,
                                                                    icon: 'none',
                                                                    duration: 5000
                                                                });
                                                                //return;
                                                            } else {
                                                                wx.showToast({
                                                                    title: '注册成功',
                                                                    icon: 'none',
                                                                    duration: 5000
                                                                })
                                                                wx.setStorageSync("userPhone", this.data.phone);
                                                                wx.redirectTo({
                                                                  url: '/pages/driver/personalInformation/personalInformation',
                                                                })
                                                            }
                                                        }
                                                    })
                                                }
                                            }
                                        })
                                        /*  */
                                    }
                                })
                            }
                        })
                    }
                }
            })
            //wx.setStorageSync("userPhone", this.data.phone);
            //handler.intoPageHandler("../../driver/personalInformation/personalInformation?url_type=register");
        } else {
            !agreement.stauts && this.agreementAndReadTest();
            !passwordTest && this.passwordTest(phone);
            !verificationcodeTest && this.verificationTest(verificationcode);
            !phoneTest && this.phoneTest(phone);
        }
    },
    // agreementAndReadHandler 我已阅读并同意事件
    agreementAndReadHandler: function() {
        const {
            agreement
        } = this.data;
        if (agreement.status) {
            this.setData({
                agreement: {
                    stauts: false,
                    image: "../../../../images/driver/icon-34.png"
                }
            });
        } else {
            this.setData({
                agreement: {
                    stauts: true,
                  image: "http://run.4hl.cn/addons/sd_drviing/core/public/WeChat/driver/icon-35.png"
                }
            });
        }
    },
    agreementAndReadTest: function() {
        const {
            agreement
        } = this.data;
        if (!agreement.stauts) {
            wx.showToast({
                title: ' 请勾选我已阅读并同意《代驾服务协议》',
                icon: "none",
                duration: 5000
            })
            return;
        }
    },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})